package ch.ethz.fcl.metrobuzz.render.od.flow_view.interactions;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import ch.ethz.fcl.metrobuzz.render.od.flow_view.sankey.ODSankeyDiagramData;
import ch.ethz.fcl.metrobuzz.render.od.flow_view.sankey.SankeyNodeGroup;

public class ODPopupMenuMouseListener extends MouseAdapter {
	private ODSankeyDiagramData data;
	private ODPopupMenu menu;

	public ODPopupMenuMouseListener(ODSankeyDiagramData data, ODPopupMenu menu) {
		this.data = data;
		this.menu = menu;
	}

	@Override
	public void mousePressed(MouseEvent me) {
		if (me.getButton() != MouseEvent.BUTTON3)
			return;

		menu.show(me.getComponent(), me.getX(), me.getY());

		if (data == null || data.getInitDestinSNList() == null)
			return;

		menu.selectedGroup = null;

		for (SankeyNodeGroup sng : data.getCurOrginSNList())
			if (sng.getBound().contains(me.getX(), me.getY())) {
				menu.selectedGroup = sng;

				break;
			}

		if (menu.selectedGroup == null)
			for (SankeyNodeGroup sng : data.getCurDestinSNList())
				if (sng.getBound().contains(me.getX(), me.getY())) {
					menu.selectedGroup = sng;
					break;
				}
	}
}